<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/24
 * Time: 11:17
 */

namespace app\qgroup\controller;


use app\common\service\BaseService;
use app\common\util\ReturnMsgUtil;
use app\qgroup\model\QqCodeMapping;
use app\qgroup\service\CodeFactory;
use app\qgroup\service\TulingService;
use think\Controller;
use think\Exception;
use think\Model;

class Base extends Controller
{
    use ReturnMsgUtil;

    protected function initialize()
    {
//        $this->verification();
    }

    /**
     * 验证请求
     * @throws \think\exception\DbException
     */
    protected function verification()
    {
        $token = input('token');
        if (!$token){
            exit($this->errorInfo('非法请求 CODE:NT'));
        }
        $tok = implode('',explode('-',$token));
        $cf = CodeFactory::getInstance();
        $qq = $cf->getDecCode($tok);
        trace($qq,'decode,qq');
        if (!$qq){
            exit($this->errorInfo('非法请求 CODE:NQ'));
        }
        if (!is_numeric($qq)){
            exit($this->errorInfo('非法请求 CODE:NNQ'));
        }
        $qcm = QqCodeMapping::get(['qq'=>$qq]);
        if (!$qcm){
            exit($this->errorInfo('未注册QQ CODE:'));
        }
        if (!$qcm->status){
            exit($this->errorInfo('未认证QQ CODE:'));
        }
        if (!$qcm->activation_state){
            exit($this->errorInfo('未激活QQ'));
        }
    }


    /**
     * @param $name
     * @return Model|BaseService|TulingService
     * @throws Exception
     */
    public function __get($name)
    {
        // TODO: Implement __get() method.
        if (isStringInString($name,'model')){
            $structure = 'model';
        }
        if (isStringInString($name,'service')){
            $structure = 'service';
        }
        $modelClassName = getCurrentModular(get_called_class()).'\\'.$structure.'\\'.substr($name,strlen($structure));
        if (class_exists($modelClassName)) {
            return new $modelClassName();
        }else{
            $modelClassName = getCommonModular().'\\'.$structure.'\\'.substr($name,strlen($structure));
            if (class_exists($modelClassName)){
                return  new $modelClassName();
            }
            throw new Exception('The class is not exist:'.$modelClassName);
        }
    }
}